// DialogEmbeddable.cpp : implementation file
//

#include "stdafx.h"
#include "DialogEmbeddable.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogEmbeddable dialog


CDialogEmbeddable::CDialogEmbeddable(UINT uResourceID, CWnd* pParent /*=NULL*/)
	: CDialog(uResourceID, pParent)
	, m_pNormalParent(pParent)
{
	//{{AFX_DATA_INIT(CDialogEmbeddable)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_pEmbeddedParent = NULL;
	m_dwNormalStyle = 0;
	m_dwNormalExStyle = 0;
	m_ifNormalVisible = TRUE;
}


void CDialogEmbeddable::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogEmbeddable)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogEmbeddable, CDialog)
	//{{AFX_MSG_MAP(CDialogEmbeddable)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogEmbeddable message handlers

BOOL CDialogEmbeddable::Create(UINT nIDTemplate, CWnd* pParentWnd) 
{
	m_pEmbeddedParent = pParentWnd;

	return CDialog::Create(nIDTemplate, pParentWnd);
}

int CDialogEmbeddable::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;

	Embed();

	return 0;
}

void CDialogEmbeddable::OnOK()
{
	Embed();
}

void CDialogEmbeddable::OnCancel()
{
	Embed();
}

/////////////////////////////////////////////////////////////////////////////
// Implementation

void CDialogEmbeddable::Embed()
{
	m_dwNormalStyle = GetStyle();
VERIFY(TRUE ==
	ModifyStyle( WS_POPUPWINDOW |WS_CAPTION |WS_THICKFRAME, WS_CHILD |WS_CLIPSIBLINGS )	);
	m_dwNormalExStyle = GetExStyle();
VERIFY(TRUE ==
	ModifyStyleEx( WS_EX_DLGMODALFRAME , 0 )	);

	m_pNormalParent = GetParent();
ASSERT(NULL != m_pNormalParent);
ASSERT(NULL != m_pEmbeddedParent);
VERIFY(m_pEmbeddedParent ==
	SetParent( m_pEmbeddedParent )	);

	m_ifNormalVisible = IsWindowVisible();

	GetWindowRect( m_rectNormal );
	CRect rectParent;
		CWnd *pParent= GetParent();
ASSERT(NULL != pParent);
		pParent->GetWindowRect( &rectParent );
		pParent->ScreenToClient( &rectParent );
ASSERT(pParent == m_pEmbeddedParent);
	MoveWindow( &rectParent );
}

void CDialogEmbeddable::Retract()
{
	SetWindowLong( this->GetSafeHwnd(), GWL_STYLE, m_dwNormalStyle );
	SetWindowLong( this->GetSafeHwnd(), GWL_EXSTYLE, m_dwNormalExStyle );
	
VERIFY(m_pNormalParent ==
	SetParent( m_pNormalParent )	);

	if (FALSE == m_ifNormalVisible)
		ShowWindow( SW_HIDE );

	SetWindowPos( &wndTopMost, m_rectNormal.left, m_rectNormal.top, m_rectNormal.Width(), m_rectNormal.Height(), SWP_SHOWWINDOW );

	CWnd *pParent= GetParent();
ASSERT(NULL != pParent);
	if (0 != (pParent->GetStyle() & WS_CLIPCHILDREN))
		pParent->RedrawWindow();
}
